#include "cBicho.h"
#include "cScene.h"


cBicho::cBicho(void)
{
	alpha_rotate = 0;
	//health = 1000;
	alive = true;
	state_tick = 0;
}


cBicho::~cBicho(void)
{
}

void cBicho::setX(GLfloat x){
	this->x = x;
}

void cBicho::setZ(GLfloat z){
	this->z = z;
}

void cBicho::setAlpaRotate(GLfloat alpha){
	this->alpha_rotate = alpha;
}

GLfloat cBicho::getX(){
	return x;
}

GLfloat cBicho::getZ(){
	return z;
}

GLfloat cBicho::getAlphaRotate(){
	return alpha_rotate;
}

void cBicho::setSpeed(float speed){
	this->speed = speed;
}

GLfloat cBicho::getSpeed(){
	return speed;
}

void cBicho::setRunning(bool running){
	this->running = running;
}

bool cBicho::getRunning(){
	return running;
}

void cBicho::RenderBicho(float n){
	int state = GetAnimationState();
	++state_tick;
	if(!alive) SetAnimation(ANIM_DEATH_FALLBACK);
	switch(state){
		case ANIM_RUN:
			Render(0,0,0,  90,0,  40 + (int)n% 6,1,	 1,1,  0,0,0);
			break;
		case ANIM_STAND:
			Render(0,0,0,  90,0,   0 + (int)n%40,1,  1,1,  0,0,0);
			break;

		case ANIM_FLIP:
			Render(0,0,0,  90,0,   72 + (int)n%12,1,  1,1,  0,0,0);
			break;

		case ANIM_ATTACK:
			Render(0,0,0,  90,0,   46 + (int)((state_tick)/10)%8,1,  1,1,  0,0,0);
			if(state_tick >= 80){
				SetAnimation(ANIM_STAND);
				state_tick = 0;
			}
			break;

		case ANIM_DEATH_FALLBACK:
			if(state_tick < 70){
				Render(0,0,0,  90,0,   178 + (int)(state_tick/10)/*%6*/,1,  1,1,  0,0,0);
			}
			else {
				Render(0,0,0,  90,0,   183,1,  1,1,  0,0,0);
			}
			break;

		case ANIM_PAIN_A:
			Render(0,0,0,  90,0,   54 + (int)(state_tick/10)%4,1,  1,1,  0,0,0);
			if(state_tick >= 80) {
				SetAnimation(ANIM_STAND);
				state_tick = 0;
			}
			break;

		case ANIM_BOOM:
			Render(0,0,0,  90,0,   198,1,  1,1,  0,0,0);
			break;
	}
}

void cBicho::RenderBoundingBox (bool b) {
	setBoundingBoxModel(b);
}

void cBicho::EixCoordenades() {
	this->x = 0.0;
	this->z = 0.0;
}

void cBicho::setStateTick(int tick){
	state_tick = tick;
}

int cBicho::getStateTick(){
	return state_tick;
}

void cBicho::incrAttackTick(){
	++state_tick;
}


GLfloat cBicho:: getMaxXBoundingBoxBicho() {
	return getMaxXBoundingBox();
}

GLfloat cBicho:: getMinXBoundingBoxBicho() {
	return getMinXBoundingBox();
}

GLfloat cBicho:: getMaxYBoundingBoxBicho() {
	return getMaxYBoundingBox();
}

GLfloat cBicho:: getMinYBoundingBoxBicho() {
	return getMinYBoundingBox();
}

GLfloat cBicho:: getMaxZBoundingBoxBicho() {
	return getMaxZBoundingBox();
}

GLfloat cBicho:: getMinZBoundingBoxBicho() {
	return getMinZBoundingBox();
}

bool cBicho::Collides(int* map){
	if ((z < -SCENE_DEPTH*TILE_SIZE) || (z > 0) || (x < 0) || (x >SCENE_WIDTH*TILE_SIZE)) {
		z *= 2;
		return true;
	}
	//UpdateBoundingBox();
	int tile_x, tile_z;

	tile_x =  x/ TILE_SIZE;
	tile_z = -z/ TILE_SIZE;

	
	GLfloat auxT = (float) TILE_SIZE;
	GLfloat auxXBox = Box.xmax;	GLfloat auxXMax2 = auxXBox;
	GLfloat auxZBox = Box.zmax;	GLfloat auxZMax2 = auxZBox;
	GLfloat auxXMinBox = Box.xmin;	GLfloat auxXMin2 = auxXMinBox;
	GLfloat auxZMinBox = Box.zmin;	GLfloat auxZMin2 = auxZMinBox;

	GLfloat auxXmax = auxXMax2 / auxT;	int XmaxTile = (int) auxXmax;
	GLfloat auxXmin = auxXMin2 / auxT;	int XminTile = (int) auxXmin;
	GLfloat auxZmax = auxZMax2 / auxT;	int ZmaxTile = (int) auxZmax;
	GLfloat auxZmin = auxZMin2 / auxT;	int ZminTile = (int) auxZmin;


	if(map[XmaxTile + (-ZminTile*SCENE_WIDTH) ] > 0) return true;
	if(map[XmaxTile + (-ZmaxTile*SCENE_WIDTH) ] > 0) return true;
	if(map[XminTile + (-ZminTile*SCENE_WIDTH) ] > 0) return true;
	if(map[XminTile + (-ZmaxTile*SCENE_WIDTH) ] > 0) return true;

	/*if(map[tile_x + (tile_z*SCENE_WIDTH) ] != 0)	{
		return true;
    }*/

	return false;
}

bool cBicho::Collides2(int* map){
	if ((z < -SCENE_DEPTH*TILE_SIZE) || (z > 0) || (x < 0) || (x >SCENE_WIDTH*TILE_SIZE)) {
		z *= 2;
		return true;
	}
	//UpdateBoundingBox();
	int tile_x, tile_z;

	tile_x =  x/ TILE_SIZE;
	tile_z = -z/ TILE_SIZE;

	if(map[tile_x + (tile_z*SCENE_WIDTH) ] != 0)	{
		return true;
    }

	return false;
}

void cBicho::setBoundingBox(cBoundingBox b){
	this->Box = b;
}

cBoundingBox cBicho::getBoundingBox(){
	return Box;
}

void cBicho::decrHealth(){
	health -= 100;
	SetAnimation(ANIM_PAIN_A);
	if (health <= 0){
		alive = false;
		SetAnimation(ANIM_DEATH_FALLBACK);
		state_tick = 0;
		Sleep(10);
		UpdateBoundingBox();
	}
}

void cBicho::UpdateBoundingBox(){
	GLfloat xmaxE1, xminE1, ymaxE1, yminE1, zmaxE1, zminE1;
	
	xmaxE1 = getMaxXBoundingBox() + getX();	Box.setXmax(xmaxE1);
	xminE1 = getMinXBoundingBox() + getX();	Box.setXmin(xminE1);
	//ymaxE1 = enemy1.getMaxYBoundingBox();
	//yminE1 = enemy1.getMinYBoundingBox();
	zmaxE1 = getMaxZBoundingBox() + getZ();	Box.setZmax(zmaxE1);
	zminE1 = getMinZBoundingBox() + getZ();	Box.setZmin(zminE1);
}

bool cBicho::CollidesBox(cBoundingBox boxE){
	if(Box.getXmin() < boxE.getXmax() && boxE.getXmin() < Box.getXmax() && 
		//	yminP1 < ymaxE1 && yminE1 < ymaxP1 
		Box.getZmin() < boxE.getZmax() && boxE.getZmin() < Box.getZmax()) return true;
	return false;
}

int cBicho:: getLife() {
	return this->life;
}

void cBicho::setLife(int life2) {
	this->life = life2;
}

int cBicho::getTileX(){
	return x/TILE_SIZE;
}

int cBicho::getTileZ(){
	return z/TILE_SIZE;
}

bool cBicho::getAlive(){
	return alive;
}

void cBicho::setHealth(int health2) {
	this->health = health2;
}

int cBicho::getHealth() {
	return this->health;
}